import java.lang.Exception

class StartUp {

    fun main(param: String) {
        print(param)

    }

    fun sum(a: Int, b: Int): Int {
        return a + b
    }

    /**
     * 可以将表达式作为函数体
     */
    fun sum1(a: Int, b: Int) = a + b

    fun printSum(a: Int, b: Int): Unit {
        println("sum of $a + $b = ${a + b}")
    }

    private fun parseInt(str: String): Int? {
        try {
            str.toInt()
        } catch (e: Exception) {
            return null
        }
        return null
    }

    fun printProduct(arg1: String, arg2: String) {
        val x = parseInt(arg1)
        val y = parseInt(arg2)

        if (x != null && y != null) {
            println(x * y) //在检测非null之后,x 和 y 会转换成 non-nullable
        } else {
            print("$x or $y is not a number")
        }
    }

    fun array() {
        val fruits = listOf("banana", "avocado", "apple", "kiwifruit")
        when {
            "banana" in fruits -> println("banana in fruits")
            "avocado" in fruits -> println("avocado in fruits")
        }

        val list = listOf(1, 4, 2, 2)
        list.sortedBy { it - 2 }

        fruits
//            .filter { it.startsWith("a") }
            .sortedBy { it.endsWith("o") }
            .map { it.toUpperCase() }
            .apply {  }
            .forEach { println(it) }


    }

    fun filter() {
        val list = listOf(1, 9, 2, 8, 3, 7, 4, 6, 5)
        val filter = list.filter { it > 3 }.sortedBy { -it }
        print(filter)

    }

    fun map() {
        val map = mapOf("a" to 1, "b" to 2, "c" to 3)
        val map1 = mapOf(Pair("name", "zhangsan"), Pair("age", 20), Pair("gender", "男"))
        println(map)
        println(map1)

        println("a = " + map["a"])
        println("name = " + map1["name"])
    }


    /**
     * 扩展属性
     */
    private fun String.toLLowCase() : String{
        return ""
    }

    fun extendFun(args : String) : String{
       return args.toLLowCase()
    }




}